package sbc.group1.lab1.mozartspaces;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.mozartspaces.core.ContainerReference;
import org.mozartspaces.core.MzsCoreException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import sbc.group1.lab1.common.Car;
import sbc.group1.lab1.common.TestStatus;
import sun.misc.Signal;
import sun.misc.SignalHandler;

class TesterDefects extends AbstractTester{
    
    public static final Logger log = LoggerFactory.getLogger(TesterDefects.class);
    
    public static void main(String[] args) throws MzsCoreException, URISyntaxException, IOException, InterruptedException {
        TesterDefects tester= new TesterDefects(new File(args[0]).toURI().toURL(), new URI(args[1]));
        final Thread t = new Thread(tester, "tester-defects");
        Signal.handle(new Signal("INT"), 
                new SignalHandler() {
                    @Override
                    public void handle(Signal sig) {
                        t.interrupt();
                    }
                });
        
        if (System.getProperty("benchmark") != null && System.getProperty("benchmark").equals("true")) {
            Signal.handle(new Signal("HUP"), 
                    new SignalHandler() {
                        @Override
                        public void handle(Signal sig) {
                            t.start();
                        }
                    });
        }else {
            t.start();
            System.in.read();
            t.interrupt();
            t.join();
        }
    }

    public TesterDefects(URL configFile, URI spaceURI) throws MzsCoreException {
        super(configFile, spaceURI);
    }
    
    @Override
    protected void markCar(Car car) {
        if (car.isDefect()) {
            car.markAsDefect();
            car.setDefectTestStatus(TestStatus.FAILED);
        }else {
            car.setDefectTestStatus(TestStatus.PASSED);
        }
    }

    @Override
    protected ContainerReference getPartiallyTestedInContainer() {
        return space.getCompletenessTestedContainer();
    }

    @Override
    protected ContainerReference getPartiallyTestedOutContainer() {
        return space.getDefectsTestedContainer();
    }



}
